/**************************************************************************************
*  LcdTest.java
*  Program demonstrating the use LCD with LPC21xx.
**************************************************************************************
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Created by:
*      RTJ Computing
**************************************************************************************/
import lpc2xxx.edu.*;
import lpc2xxx.hw.*;


class LcdTest
{
    static Switch sw = new Switch();
    static boolean backlightOn = true;
    static LCD lcd = new LCD(2, 16);

    static void main(String[] args) 
    {
        Console.println("");
        Console.println("LPC21xx LCD 4-bit control demo");
        Console.println("This program is for use with LPC2148 Education board");
        Console.println("Jumper settings:");
        Console.println("  disconnect J25,J26,J27,J28,J39");
        Console.println("  connect    J29,J30,J31,J32 J38,J40,J41\r\n");

        sw.start();

        for (int i=0; i < 3; i++)
        {
            lcd.backlightOn();
            delay(100);
            lcd.backlightOff();
            delay(100);
        }
        lcd.backlightOn();
/*
        lcd.showAt(LCD.LINE2, 0, 'P');
        lcd.showAt(LCD.LINE2, 1, 'E');
        lcd.showAt(LCD.LINE2, 2, 'T');
        lcd.showAt(LCD.LINE2, 3, 'E');
        lcd.showAt(LCD.LINE2, 4, 'R');
        //lcd.showAt(LCD.LINE2,  0, 'G');
        //lcd.showAt(LCD.LINE2, 15, 'a');
*/        
        lcd.showLine(LCD.LINE1, "simpleRTJ", LCD.CENTER);
        lcd.showLine(LCD.LINE2, "LPC2148 EduBoard", LCD.LEFT);

        showMenu();
        while (true)
        {
            char key = Console.getKey();
            if (key == Console.ESC)
                break;

            switch (key)
            {
                case '1':
                    dispText(LCD.LINE1);
                    showMenu();
                    break;

                case '2':
                    dispText(LCD.LINE2);
                    showMenu();
                    break;

                case '3':
                    toggleBacklight();
                    break;

            }

            if (sw.sw2())
            {
                toggleBacklight();
                while (sw.sw2())
                    Thread.yield();
            }

        }
        lcd.backlightOff();
        lcd.clearDisp();
    }

    static void dispText(int line)
    {
        Console.print("Enter text for line " + line + ": ");
        String s = Console.getKeys();
        if (s != null && s.length() > 0)
            lcd.showLine(line, s, LCD.CENTER);
    }

    static void showMenu()
    {
        Console.println("");
        Console.println("Press:");
        Console.println("   1   - enter text for line 1");
        Console.println("   2   - enter text for line 2");
        Console.println("   3 (or SW2) - toggle LCD backlight");
        Console.println("   ESC - exit program");
    }

    static void toggleBacklight()
    {
        if (backlightOn)
            lcd.backlightOff();
        else
            lcd.backlightOn();
        backlightOn = !backlightOn;
    }
    
    static void delay(int ms)
    {
        try
        {
            Thread.sleep(ms);
        }
        catch (InterruptedException e) {}
    }
}

